package com.mercury.coffee.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mercury.coffee.entity.CoffeeTypeVo;
import com.mercury.coffee.entity.PushCoffeeType;
import com.mercury.coffee.service.PushCoffeeTypeService;
import com.mercury.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 *
 * @author 童雅婷
 * @since 2021-11-07
 */
@RestController
@RequestMapping("/coffee/coffee-type")
@Api(value = "咖啡标签控制器")
@CrossOrigin
public class PushCoffeeTypeController {

    @Autowired
    private PushCoffeeTypeService coffeeTypeService;

    @ApiOperation(value = "添加咖啡豆标签")
    @PostMapping("/addType")
    public R addType(@RequestBody PushCoffeeType coffeeType) {
        coffeeType.setDeleted(false);
        coffeeTypeService.save(coffeeType);
        return R.ok().message("添加成功");
    }

    @ApiOperation(value = "获得所有咖啡豆标签")
    @GetMapping("/typeList")
    public R getList() {
        List<PushCoffeeType> list = coffeeTypeService.list(null);
        return R.ok().data("items", list);
    }

    @ApiOperation("分页展示咖啡豆标签")
    @GetMapping("/typePage/{current}/{limit}")
    public R pageType(@PathVariable long current,
                      @PathVariable long limit) {
        Page<PushCoffeeType> page = new Page<>(current, limit);
        coffeeTypeService.page(page, null);
        long total = page.getTotal();
        List<PushCoffeeType> records = page.getRecords();
        return R.ok().data("items", records).data("total", total);
    }

    @ApiOperation("删除咖啡豆标签")
    @DeleteMapping("deleteType/{id}")
    public R deleteType(@PathVariable int id) {
        boolean res = coffeeTypeService.removeById(id);
        if (res) return R.ok().message("删除成功");
        else return R.error().message("删除失败");
    }

    /**
     * @date 2021/11/10 11:50
     * @return
     * @description 首先获得父类型，然后根据父 id 获得所有的子类型
    */
    @ApiOperation("按照获得的父类型分组获得所有标签")
    @GetMapping("getTypeGroup")
    public R getTypeGroup() {
        List<CoffeeTypeVo> typeGroup = coffeeTypeService.getByGroup();
        return R.ok().data("typeGroup", typeGroup);
    }


}

